﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

    <link rel="stylesheet" href="/css/themes/default/jquery.mobile-1.4.2.min.css" />
    <script src="/js/jquery.js"></script>
    <script src="/js/jquery.mobile-1.4.2.min.js"></script>
    <script src="/js/main.js"></script>
    
</head>
<body>
    <div data-role="page" class="jqm-demos" id="panel-fixed-page1" data-title="">

        <div data-role="header" data-position="fixed">
            <h1 id="title_h1"></h1>
        </div>
        <!-- /header -->

        <div role="main" class="ui-content jqm-content jqm-fullwidth">

            <form id="main_form">
            </form>

            <input type="hidden"
        </div>
        <!-- /content -->

        <div data-role="footer" data-position="fixed">
            <div data-role="navbar" data-iconpos="left">
                <ul>
                    <li><a href="/" data-icon="back">取消</a></li>
                    <li><a id="form_submit" href="#" data-icon="check" class="ui-btn-active">提交</a></li>
                </ul>
            </div>
            <!-- /navbar -->
        </div>
        <!-- /footer -->



    </div>
    <!-- /page -->

    <script>
        $(function () {
            var formCode = $.getUrlParam("FormCode");
            var form = {};
            $.get("/FormDataHandler.ashx?FormCode=" + formCode, function (result) {
                eval("form=" + result);
                $("#title_h1").text(form.Name);
                $("title").text(form.Name);
                var container = $("#main_form");
                for (var i = 0; i < form.Items.length; i++) {
                    var item = form.Items[i];
                    if (item.ControlType == "radio") {
                        var fieldset = $("<fieldset data-role='controlgroup' />").append($("<legend>" + item.Name + ":</legend>"));
                        for (var j = 0; j < item.ControlData.length; j++) {
                            var ds = item.ControlData[j];
                            var input = $("<input />").attr({ type: "radio", value: ds[0], name: item.Code, id: item.Code + j });
                            var label = $("<label />").attr("for", item.Code + j).text(ds[1]);
                            fieldset.append(input).append(label);
                        }
                        container.append(fieldset);
                    }
                    else if (item.ControlType == "textarea") {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<textarea />").attr({ rows: "4", name: item.Code, id: item.Code });
                        container.append(label).append(input);
                    }
                    else {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<input />").attr({ type: "text", name: item.Code, id: item.Code });
                        container.append(label).append(input);
                    }

                }
                container.trigger('create');
            });
            $("#form_submit").click(this, function () {
                var inputDict = $("#main_form").serializeJson();
                for (var i = 0; i < form.Items.length; i++) {
                    var item = form.Items[i];
                    if (item.CheckCondition != "") {
                        var checks = eval(item.CheckCondition);
                        var value = inputDict[item.Code];
                        for (var j = 0; j < checks.length; j++) {
                            if (!eval(checks[j][0])) {
                                PromptMessage(checks[j][1]);
                                return;
                            }
                        }
                    }
                }
                $.post("Handler2.ashx?FormCode=" + formCode, inputDict, function (result) {
                    PromptMessage("提交成功");
                    setTimeout(function () { location.href = "/"; }, 2000);
                    
                });
            });
            function PromptMessage(mes) {
                $("<div><p>" + mes + "</p></div>").popup().popup("open");
            }
        });
    </script>
</body>
</html>
